// pages/orderDetails/orderDetails.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderDetail: {}, //订单商品详情
    orderStatus: null, //订单状态
    orderId: null, //订单Id     
    orderStatusLine: [ //订单状态进度条
      '../../icons/icon_ing.png',
      '../../icons/icon_waitline.png',
      '../../icons/icon_wait.png',
      '../../icons/icon_waitline.png',
      '../../icons/icon_wait.png',
      '../../icons/icon_waitline.png',
      '../../icons/icon_wait.png',
      '../../icons/icon_waitline.png',
      '../../icons/icon_wait.png'
    ],
    totalFee: ""
  },

  /**
   * 打电话
   */
  phone: function() {
    wx.makePhoneCall({
      phoneNumber: this.data.orderDetail[0].phone,
    })
  },


  /**
   * 获得订单详情
   */
  getOrderDetails: function(options) {
    let that = this;
    wx.showLoading({
      title: '正在加载中',
    })
    wx.request({
      url: 'https://www.xiaozheng8.com/shop/order/findById',
      method: 'post',
      data: {
        orderId: options.orderId
      },
      success: function(res) {
        console.log(res)
        that.setData({
          orderDetail: [res.data.data],
          orderStatus: res.data.data.orderStatus,
          orderId: res.data.data.orderId,
          totalFee: res.data.data.orderMoney
        })
        wx.hideLoading()
        console.log(that.data.orderStatus)
        let newStatus = that.data.orderStatusLine;
        if (that.data.orderStatus == 1) {} else if (that.data.orderStatus == 2) {
          newStatus.splice(0, 5, '../../icons/icon_ok.png',
            '../../icons/icon_okline.png', '../../icons/icon_ok.png',
            '../../icons/icon_okline.png', '../../icons/icon_ing.png');
          that.setData({
            orderStatusLine: newStatus
          })
        } else if (that.data.orderStatus == 3) {
          newStatus.splice(0, 5, '../../icons/icon_ok.png',
            '../../icons/icon_okline.png', '../../icons/icon_ok.png',
            '../../icons/icon_okline.png', '../../icons/icon_ing.png');
          that.setData({
            orderStatusLine: newStatus
          })
        } else if (that.data.orderStatus == 4) {
          newStatus.splice(0, 7, '../../icons/icon_ok.png',
            '../../icons/icon_okline.png', '../../icons/icon_ok.png',
            '../../icons/icon_okline.png', '../../icons/icon_ok.png', '../../icons/icon_okline.png', '../../icons/icon_ing.png');
          that.setData({
            orderStatusLine: newStatus
          })
        } else if (that.data.orderStatus == 5) {
          newStatus.splice(0, 9, '../../icons/icon_ok.png',
            '../../icons/icon_okline.png', '../../icons/icon_ok.png',
            '../../icons/icon_okline.png', '../../icons/icon_ok.png', '../../icons/icon_okline.png', '../../icons/icon_ok.png', '../../icons/icon_okline.png', '../../icons/icon_ok.png');
          that.setData({
            orderStatusLine: newStatus
          })
        }
      }
    })
  },
  // 付款
  confirm: function() {
    var that = this;
    console.log(this.data.orderId);
    var total = parseFloat(this.data.totalFee) * 100;
    wx.request({
      url: 'https://www.xiaozheng8.com/shop/order/pay',
      method: "post",
      data: {
        orderId: that.data.orderId,
        totalFee: total
      },
      success: function(res) {
        console.log(res);
        wx.requestPayment({
          'timeStamp': res.data.timeStamp,
          'nonceStr': res.data.nonceStr,
          'package': res.data.package,
          'signType': 'MD5',
          "appId": res.data.appId,
          'paySign': res.data.paySign,
          success: function(res) {
            console.log('success');
            wx.showToast({
              title: '支付成功',
              icon: 'success',
              duration: 3000
            });

            wx.redirectTo({
              url: '../orderDetails/orderDetails?orderId' + that.data.orderId,
            });
            wx.showToast({
                title: '商品购买成功',
              }),

              wx.request({
                url: 'https://www.xiaozheng8.com/shop/order/setStatus',
                method: "post",
                data: {
                  orderId: that.data.orderId,
                  status: 2
                }
              })
          },
          fail: function(res) {
            console.log(res);
            console.log("取消支付");
            // ({
            //   url: '../shopping/shopping?cateId' + that.data.productId,
            // });
            wx.showToast({
              title: '商品购买失败',
              icon: "none"
            });
            wx.request({
              url: 'https://www.xiaozheng8.com/shop/order/setStatus',
              method: "post",
              data: {
                orderId: that.data.orderId,
                status: 1
              },

            })
          }
        })

      }

    })
  },
  /**
   * 确认收货
   */
  confirm: function() {
    let that = this;
    wx.showModal({
      content: '确认收货',
      cancalColor: '#999',
      confirmColor: '#0bbd79',
      success: function(res) {
        if (res.cancel) {} else {
          wx.request({
            url: 'https://www.xiaozheng8.com/shop/order/setStatus',
            method: 'post',
            data: {
              orderId: that.data.orderId,
              status: 5
            },
            success: function() {
              wx.showToast({
                title: '确认收货成功',
                icon: 'success',
                duration: 2000,
              })
              let options = {
                orderId: that.data.orderId
              }
              that.getOrderDetails(options);
            }
          })
        }
      }
    })
  },
  // 付款
  buy: function() {
    var that = this;
    console.log(that.data.orderId)
    var total = parseFloat(this.data.totalFee) * 100;
    wx.request({
      url: 'https://www.xiaozheng8.com/shop/order/pay',
      method: "post",
      data: {
        orderId: that.data.orderId,
        totalFee: total
      },
      success: function(res) {
        console.log(res);
        wx.requestPayment({
          'timeStamp': res.data.timeStamp,
          'nonceStr': res.data.nonceStr,
          'package': res.data.package,
          'signType': 'MD5',
          "appId": res.data.appId,
          'paySign': res.data.paySign,
          success: function(res) {
            console.log('success');
            wx.showToast({
              title: '支付成功',
              icon: 'success',
              success: function() {
                wx.redirectTo({
                  url: '../orderDetails/orderDetails?orderId' + that.data.orderId,
                });
                wx.showToast({
                    title: '商品购买成功',
                  }),
                  wx.request({
                    url: 'https://www.xiaozheng8.com/shop/order/setStatus',
                    method: "post",
                    data: {
                      orderId: that.data.orderId,
                      status: 2
                    }
                  })
              }
            });

          },
          fail: function(res) {
            console.log(res);
            console.log("取消支付");
            wx.showToast({
              title: '商品购买失败',
              icon: "none"
            });
            wx.request({
              url: 'https://www.xiaozheng8.com/shop/order/setStatus',
              method: "post",
              data: {
                orderId: that.data.orderId,
                status: 1
              },

            })
          }
        })
      }
    })
  },

  /**
   * 退货
   */
  return: function(e) {
    var that = this;
    var status = e.currentTarget.dataset.status;
    if (status < 2) {
      wx.showModal({
        title: '提示',
        content: '您确认要取消该订单',
        success(res) {
          if (res.confirm) {
            wx.request({
              url: 'https://www.xiaozheng8.com/shop/order/setStatus',
              data: {
                status: -1,
                orderId: that.data.orderId
              },

              method: 'post',

              success: function(res) {
                wx.navigateBack({
                  delta: 1
                })
              },
              fail: function(res) {},
              complete: function(res) {},
            })
          }
        }
      })

    } else {
      wx.showModal({
        title: '提示',
        content: '您当前不能直接退款，请联系客服',
        showCancel: false
      })

    }

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options);
    this.getOrderDetails(options)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})